﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
using MigrateAllPublicFolder;

namespace MigrateAllPublicFolder
{
    class OutlookMail
    {

        /// <summary>
        /// CONTRUCTOR
        /// </summary>
        /// <param name="OutlookItem"></param>
        public OutlookMail(object OutlookItem)
        {
            this.OutlookItem = OutlookItem;

            if (OutlookItem is Outlook.MailItem)//MAIL ITEM
            {
                Outlook.MailItem Mail = OutlookItem as Outlook.MailItem;

                try
                {
                    //filename
                    if ((Mail.Subject == null) || (Mail.Subject == "")) Subject = "NoSubject";
                    else Subject = Mail.Subject;
                    //from
                    From = (Mail.Sender != null? Mail.Sender.Name : "");
                    //to
                    int RecipientsCount = Mail.Recipients.Count;
                    if (RecipientsCount > 0)
                    {
                        string recipients = "";
                        for (int i = 1; i < Mail.Recipients.Count + 1; i++)
                            recipients += Mail.Recipients[i].Name + "; ";
                        recipients = recipients.Remove(recipients.Length - 2);
                        To = MyUtility.GetMax250chars(recipients);
                    }
                    //received
                    Received = Mail.ReceivedTime.ToString();
                    //attach
                    Attach = (Mail.Attachments.Count > 0 ? "√" : "");
                    //flagstatus
                    FlagStatus = (Mail.FlagRequest != null ? "√" : "");

                    Created = true;
                }
                catch (Exception ex)
                {
                    string Error = ex.Message;
                }
            }
            else if (OutlookItem is Outlook.ReportItem)//REPORT ITEM
            {
                Outlook.ReportItem Mail = OutlookItem as Outlook.ReportItem;
                //subject
                if ((Mail.Subject == null) || (Mail.Subject == "")) Subject = "REPORT-NoSubject";
                else Subject = MyUtility.GetStringAllowedChars("REPORT-" + Mail.Subject);

                Created = true;
            }
            else if (OutlookItem is Outlook.MeetingItem)//MEETING ITEM
            {
                Outlook.MeetingItem Mail = OutlookItem as Outlook.MeetingItem;
                //subject
                if ((Mail.Subject == null) || (Mail.Subject == "")) Subject = "MEETING-NoSubject";
                else Subject = MyUtility.GetStringAllowedChars("MEETING-" + Mail.Subject);

                Created = true;
            }
        }


        public void SaveAs(string ItemPath)
        {
            if (OutlookItem is Outlook.MailItem)//MAIL ITEM
            {
                Outlook.MailItem Mail = OutlookItem as Outlook.MailItem;
                Mail.SaveAs(ItemPath, Type.Missing);
            }
            else if (OutlookItem is Outlook.ReportItem)//REPORT ITEM
            {
                Outlook.ReportItem Mail = OutlookItem as Outlook.ReportItem;
                Mail.SaveAs(ItemPath, Type.Missing);
            }
            else if (OutlookItem is Outlook.MeetingItem)//MEETING ITEM
            {
                Outlook.MeetingItem Mail = OutlookItem as Outlook.MeetingItem;
                Mail.SaveAs(ItemPath, Type.Missing);
            }
        }


        public object OutlookItem { get; set; }
        public string Subject { get; set; }
        public string Attach { get; set; }
        public string From { get; set; }
        public string To { get; set; }
        public string Received { get; set; }
        public string FlagStatus { get; set; }
        public bool Created { get; set; }

    }
}
